package cn.wetcland.urban.framework.service.impl;

import cn.wetcland.urban.common.MyAssert;
import cn.wetcland.urban.framework.mapper.UtDistrictMapper;
import cn.wetcland.urban.framework.service.UtDistrictService;
import cn.wetcland.urban.framework.vo.DistrictListVO;
import cn.wetcland.urban.generator.domain.UtDistrict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.geom.PathIterator;
import java.util.List;

/**
* @author Wetcland
* @description 针对表【ut_district】的数据库操作Service实现
* @createDate 2024-01-21 09:51:59
*/
@Service
public class UtDistrictServiceImpl extends ServiceImpl<UtDistrictMapper, UtDistrict> implements UtDistrictService {

    @Resource
    public UtDistrictMapper districtMapper;

    @Override
    public DistrictListVO getProvinceList() {
        List<UtDistrict> utDistricts = districtMapper.selectList(new QueryWrapper<UtDistrict>().eq("level", "province"));
        DistrictListVO districtListVO = new DistrictListVO();
        districtListVO.setDistrictListItems(utDistricts);
        return districtListVO;
    }

    @Override
    public DistrictListVO getCityList(Long provinceId) {
        MyAssert.notNull(provinceId, "省Id不存在");
        List<UtDistrict> utDistricts = districtMapper.selectList(new QueryWrapper<UtDistrict>().eq("parent_id", provinceId).eq("level", "city"));
        DistrictListVO districtListVO = new DistrictListVO();
        districtListVO.setDistrictListItems(utDistricts);
        return districtListVO;
    }

    @Override
    public DistrictListVO getDistrictList(Long cityId) {
        MyAssert.notNull(cityId, "市Id不存在");
        List<UtDistrict> utDistricts = districtMapper.selectList(new QueryWrapper<UtDistrict>().eq("parent_id", cityId).eq("level", "district"));
        DistrictListVO districtListVO = new DistrictListVO();
        districtListVO.setDistrictListItems(utDistricts);
        return districtListVO;
    }

    public boolean verifyDistrict(long provinceId, long cityId, long districtId) {
        UtDistrict district1 = districtMapper.selectById(cityId);
        MyAssert.notNull(district1, "城市不存在");
        UtDistrict district2 = districtMapper.selectById(districtId);
        MyAssert.notNull(district2, "区县不存在");
        return district2.getParentId() == cityId && district1.getParentId() == provinceId;
    }
}




